package com.tmptSource.controller.action;

import com.opensymphony.xwork2.ActionSupport;
import com.tmpt.dto.Result;
import com.tmptSource.dto.OwAdvShopbrandBasicInfoDto;
import com.tmptSource.dto.PageDto;
import com.tmptSource.service.intf.IOwAdvShopbrandBasicInfoService;
import com.tmpt.utils.*;
import lombok.Getter;
import lombok.Setter;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.*;

@Getter
@Setter
@Controller(value="owAdvShopbrandBasicInfoAction")
@Scope("prototype")
public class OwAdvShopbrandBasicInfoAction extends ActionSupport {
	private Logger log = Logger.getLogger(this.getClass());


	//------------
	//与页面交互的分页和排序
	//--------------
	private int page=0;
	private int rows=0;
	private String sortColumn;
	private String sortOrder;

	//------------
	//与页面交互的业务数据
	//--------------
    private String id;
    private String advSecurityInfo;
    private String advApprovalTerm;
    private String addr;
    private String advBrandType;
    private String advContent;
    private String owner;
    private String linkman;
    private String tel;
    private String advSetMode;
    private String setYear;
    private String advApproval;
    private String advStructureType;
    private String advSetHeight;
    private String size;
    private String setNum;
    private String advMaterial;
    private String companyId;
    private String createdate;
	private String createdate_Start;
	private String createdate_End;
    private String deleteflag;
    private String createman;
    private String createmanId;
    private String modifydate;
	private String modifydate_Start;
	private String modifydate_End;
    private String modifyman;
    private String modifymanId;

	//返回结果
	private String result;
	//返回结果
	private List<Map<String,Object>> jsonArrayResult = new ArrayList<Map<String,Object>>();


	@Resource
	private IOwAdvShopbrandBasicInfoService owAdvShopbrandBasicInfoService;


	/**
	* 分页查询列表
	* @return
	*/
	public String queryListBySearch(){
		try {

			HttpServletResponse response = ServletActionContext.getResponse();
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();

			//查询参数
			//分页/排序查询条件
			PageDto pageDto = new PageDto();
			//默认主键desc
			if(StringUtils.isNull(sortColumn)){
				sortColumn = "id";
				sortOrder = "desc";
			}
			pageDto.setPageCurrent(page-1);
			pageDto.setPageSize(rows);
			pageDto.setSortColumn(sortColumn);
			pageDto.setSortOrder(sortOrder);

			//查询条件
			OwAdvShopbrandBasicInfoDto owAdvShopbrandBasicInfoParam = new OwAdvShopbrandBasicInfoDto();
			owAdvShopbrandBasicInfoParam.setAdvSecurityInfo(this.advSecurityInfo);
			owAdvShopbrandBasicInfoParam.setAdvSetMode(this.advSetMode);
			owAdvShopbrandBasicInfoParam.setAdvApproval(this.advApproval);
			owAdvShopbrandBasicInfoParam.setAdvApprovalTerm(this.advApprovalTerm);
			owAdvShopbrandBasicInfoParam.setAdvStructureType(this.advStructureType);
			owAdvShopbrandBasicInfoParam.setAdvSetHeight(this.advSetHeight);
			owAdvShopbrandBasicInfoParam.setAdvMaterial(this.advMaterial);
			owAdvShopbrandBasicInfoParam.setAdvBrandType(this.advBrandType);

			//data limit control
			String requireUrl = request.getRequestURI().toString();
			String queryString = request.getQueryString();
			String contextPath = request.getContextPath();
			//data limit parameters
			String userId = WebUtils.getUserId();
			Map<String,Object> paramMap = new HashMap<String, Object>();
			paramMap.put("userId", userId);
			paramMap.put("contextPath", contextPath);
			paramMap.put("requireUrl", requireUrl);
			paramMap.put("queryString", queryString);

			//调用远程服务
			Result<OwAdvShopbrandBasicInfoDto> resultObj =  owAdvShopbrandBasicInfoService.pagination(owAdvShopbrandBasicInfoParam,pageDto,paramMap);
			if(!resultObj.isSuccess()){
				this.result = "error:"+resultObj.getContent();
				JsonPaginationUtils.responseWriter(response, this.result);
				return "jsonResult";
			}
			Map<String,Object> resultMap = resultObj.getResultMap();

			//分页记录
			List<OwAdvShopbrandBasicInfoDto> list = (List<OwAdvShopbrandBasicInfoDto>)resultMap.get("rows");
			JSONArray jsonArrayRows = JSONArray.fromObject(list);

			//分页总记录数
			int total = 0;
			if(resultMap.get("total") != null){
				total = Integer.parseInt(resultMap.get("total").toString());
			}

			//分页合计（select '合计' as c1, sum(balance) as c2 ....）
			JSONArray jsonArrayFooter = null;
			if(resultMap.get("footer")!=null){
				jsonArrayFooter = JSONArray.fromObject(resultMap.get("footer"));
			}

			JSONObject json = new JSONObject();
			json.put("total", total);
			json.put("rows", jsonArrayRows);
			//json.put("reportSqlString",reportSqlString);
			if(jsonArrayFooter!=null){
				json.put("footer", jsonArrayFooter);
			}

			this.result = json.toString();
			JsonPaginationUtils.responseWriter(response, this.result);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "jsonResult";
	}

	/**
	* 伪删除数据
	* @return
	*/
	public String removeData(){
		Result resultObj = new Result();
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();

			//参数
			String adminMan = WebUtils.getUsername();//加入管理员

			//调用远程服务
			resultObj =  owAdvShopbrandBasicInfoService.deleteById(Long.valueOf(id),adminMan);
			this.jsonArrayResult.add(resultObj.getResultMap());

		} catch (Exception e) {
			e.printStackTrace();
			resultObj.fault("error:系统出错:"+e.getLocalizedMessage());
			this.jsonArrayResult.add(resultObj.getResultMap());
		}

		return "jsonArray";
	}


	/**
	* 编辑数据
	* @return
	*/
	public String editData(){
		Result<OwAdvShopbrandBasicInfoDto> resultObj = new Result();
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();

			//参数
			OwAdvShopbrandBasicInfoDto owAdvShopbrandBasicInfoDto = new OwAdvShopbrandBasicInfoDto();
			String adminMan = WebUtils.getUsername();//加入管理员
            String userId = WebUtils.getUserId();
			owAdvShopbrandBasicInfoDto.setModifymanId(userId);
			owAdvShopbrandBasicInfoDto.setModifyman(adminMan);
			owAdvShopbrandBasicInfoDto.setCompanyId(WebUtils.getCompanyId());
			owAdvShopbrandBasicInfoDto.setId(this.id);
			//编辑数据
			owAdvShopbrandBasicInfoDto.setAdvApprovalTerm(this.advApprovalTerm);
			owAdvShopbrandBasicInfoDto.setAddr(this.addr);
			//去空格
			this.advBrandType = this.advBrandType.replaceAll(" ","");
			owAdvShopbrandBasicInfoDto.setAdvBrandType(this.advBrandType);
			owAdvShopbrandBasicInfoDto.setAdvContent(this.advContent);
			owAdvShopbrandBasicInfoDto.setOwner(this.owner);
			owAdvShopbrandBasicInfoDto.setLinkman(this.linkman);
			owAdvShopbrandBasicInfoDto.setTel(this.tel);
			owAdvShopbrandBasicInfoDto.setAdvSetMode(this.advSetMode);
			owAdvShopbrandBasicInfoDto.setSetYear(this.setYear);
			owAdvShopbrandBasicInfoDto.setAdvApproval(this.advApproval);
			owAdvShopbrandBasicInfoDto.setAdvStructureType(this.advStructureType);
			owAdvShopbrandBasicInfoDto.setAdvSetHeight(this.advSetHeight);
			owAdvShopbrandBasicInfoDto.setSize(this.size);
			owAdvShopbrandBasicInfoDto.setSetNum(this.setNum);
			owAdvShopbrandBasicInfoDto.setAdvSecurityInfo(this.advSecurityInfo);
			this.advMaterial = this.advMaterial.replaceAll(" ","");
			owAdvShopbrandBasicInfoDto.setAdvMaterial(this.advMaterial);

			//调用远程服务
			resultObj =  owAdvShopbrandBasicInfoService.editData(owAdvShopbrandBasicInfoDto);
			this.jsonArrayResult.add(resultObj.getResultMap());

		} catch (Exception e) {
			e.printStackTrace();
			resultObj.fault("error:系统出错:"+e.getLocalizedMessage());
			this.jsonArrayResult.add(resultObj.getResultMap());
		}

		return "jsonArray";
	}

	/**
	* 添加数据
	* @return
	*/
	public String addData(){
		Result<OwAdvShopbrandBasicInfoDto> resultObj = new Result();
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();

			//插入参数
			OwAdvShopbrandBasicInfoDto owAdvShopbrandBasicInfoDto = new OwAdvShopbrandBasicInfoDto();
			String adminMan = WebUtils.getUsername();//加入管理员
			String userId = WebUtils.getUserId();
			owAdvShopbrandBasicInfoDto.setCreatemanId(userId);
			owAdvShopbrandBasicInfoDto.setCreateman(adminMan);
			owAdvShopbrandBasicInfoDto.setCompanyId(WebUtils.getCompanyId());
			//插入数据
			owAdvShopbrandBasicInfoDto.setAdvApprovalTerm(this.advApprovalTerm);
			owAdvShopbrandBasicInfoDto.setAddr(this.addr);
			//去空格
			this.advBrandType = this.advBrandType.replaceAll(" ","");
			owAdvShopbrandBasicInfoDto.setAdvBrandType(this.advBrandType);
			owAdvShopbrandBasicInfoDto.setAdvContent(this.advContent);
			owAdvShopbrandBasicInfoDto.setOwner(this.owner);
			owAdvShopbrandBasicInfoDto.setLinkman(this.linkman);
			owAdvShopbrandBasicInfoDto.setTel(this.tel);
			owAdvShopbrandBasicInfoDto.setAdvSetMode(this.advSetMode);
			owAdvShopbrandBasicInfoDto.setSetYear(this.setYear);
			owAdvShopbrandBasicInfoDto.setAdvApproval(this.advApproval);
			owAdvShopbrandBasicInfoDto.setAdvStructureType(this.advStructureType);
			owAdvShopbrandBasicInfoDto.setAdvSetHeight(this.advSetHeight);
			owAdvShopbrandBasicInfoDto.setSize(this.size);
			owAdvShopbrandBasicInfoDto.setSetNum(this.setNum);
			owAdvShopbrandBasicInfoDto.setAdvSecurityInfo(this.advSecurityInfo);
			this.advMaterial = this.advMaterial.replaceAll(" ","");
			owAdvShopbrandBasicInfoDto.setAdvMaterial(this.advMaterial);

			//调用远程服务
			resultObj =  owAdvShopbrandBasicInfoService.addData(owAdvShopbrandBasicInfoDto);
			this.jsonArrayResult.add(resultObj.getResultMap());

		} catch (Exception e) {
			e.printStackTrace();
			resultObj.fault("error:系统出错:"+e.getLocalizedMessage());
			this.jsonArrayResult.add(resultObj.getResultMap());
		}
		return "jsonArray";
	}


	//=====================================================
	// setter getter method
	//=====================================================


	public String getId(){
	return id;
	}
	public void setId(String id){
	this.id = id;
	}
	public String getAdvApprovalTerm(){
	return advApprovalTerm;
	}
	public void setAdvApprovalTerm(String advApprovalTerm){
	this.advApprovalTerm = advApprovalTerm;
	}
	public String getAddr(){
	return addr;
	}
	public void setAddr(String addr){
	this.addr = addr;
	}
	public String getAdvBrandType(){
	return advBrandType;
	}
	public void setAdvBrandType(String advBrandType){
	this.advBrandType = advBrandType;
	}
	public String getAdvContent(){
	return advContent;
	}
	public void setAdvContent(String advContent){
	this.advContent = advContent;
	}
	public String getOwner(){
	return owner;
	}
	public void setOwner(String owner){
	this.owner = owner;
	}
	public String getLinkman(){
	return linkman;
	}
	public void setLinkman(String linkman){
	this.linkman = linkman;
	}
	public String getTel(){
	return tel;
	}
	public void setTel(String tel){
	this.tel = tel;
	}
	public String getAdvSetMode(){
	return advSetMode;
	}
	public void setAdvSetMode(String advSetMode){
	this.advSetMode = advSetMode;
	}
	public String getSetYear(){
	return setYear;
	}
	public void setSetYear(String setYear){
	this.setYear = setYear;
	}
	public String getAdvApproval(){
	return advApproval;
	}
	public void setAdvApproval(String advApproval){
	this.advApproval = advApproval;
	}
	public String getAdvStructureType(){
	return advStructureType;
	}
	public void setAdvStructureType(String advStructureType){
	this.advStructureType = advStructureType;
	}
	public String getAdvSetHeight(){
	return advSetHeight;
	}
	public void setAdvSetHeight(String advSetHeight){
	this.advSetHeight = advSetHeight;
	}
	public String getSize(){
	return size;
	}
	public void setSize(String size){
	this.size = size;
	}
	public String getSetNum(){
	return setNum;
	}
	public void setSetNum(String setNum){
	this.setNum = setNum;
	}
	public String getAdvSecurityInfo(){
	return advSecurityInfo;
	}
	public void setAdvSecurityInfo(String advSecurityInfo){
	this.advSecurityInfo = advSecurityInfo;
	}
	public String getAdvMaterial(){
	return advMaterial;
	}
	public void setAdvMaterial(String advMaterial){
	this.advMaterial = advMaterial;
	}
	public String getCompanyId(){
	return companyId;
	}
	public void setCompanyId(String companyId){
	this.companyId = companyId;
	}
	public String getCreatedate(){
	return createdate;
	}
	public void setCreatedate(String createdate){
	this.createdate = createdate;
	}
    public String getCreatedate_Start(){
    	return createdate_Start;
    }
    public void setCreatedate_Start(String createdate_Start){
    	this.createdate_Start = createdate_Start;
    }
    public String getCreatedate_End(){
    	return createdate_End;
    }
    public void setCreatedate_End(String createdate_End){
    	this.createdate_End = createdate_End;
    }
	public String getDeleteflag(){
	return deleteflag;
	}
	public void setDeleteflag(String deleteflag){
	this.deleteflag = deleteflag;
	}
	public String getCreateman(){
	return createman;
	}
	public void setCreateman(String createman){
	this.createman = createman;
	}
	public String getCreatemanId(){
	return createmanId;
	}
	public void setCreatemanId(String createmanId){
	this.createmanId = createmanId;
	}
	public String getModifydate(){
	return modifydate;
	}
	public void setModifydate(String modifydate){
	this.modifydate = modifydate;
	}
    public String getModifydate_Start(){
    	return modifydate_Start;
    }
    public void setModifydate_Start(String modifydate_Start){
    	this.modifydate_Start = modifydate_Start;
    }
    public String getModifydate_End(){
    	return modifydate_End;
    }
    public void setModifydate_End(String modifydate_End){
    	this.modifydate_End = modifydate_End;
    }
	public String getModifyman(){
	return modifyman;
	}
	public void setModifyman(String modifyman){
	this.modifyman = modifyman;
	}
	public String getModifymanId(){
	return modifymanId;
	}
	public void setModifymanId(String modifymanId){
	this.modifymanId = modifymanId;
	}

	public String getSortColumn() {
		return sortColumn;
	}
	public void setSortColumn(String sortColumn) {
		this.sortColumn = sortColumn;
	}
	public String getSortOrder() {
		return sortOrder;
	}
	public void setSortOrder(String sortOrder) {
		this.sortOrder = sortOrder;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public List<Map<String, Object>> getJsonArrayResult() {
		return jsonArrayResult;
	}
	public void setJsonArrayResult(List<Map<String, Object>> jsonArrayResult) {
		this.jsonArrayResult = jsonArrayResult;
	}

}
